<template>
  <div class="campus-enterprise-info">
    <el-container>
      <el-header>
        <span>{{ enterpriseUserForm.hname }}---个人信息</span>
      </el-header>
    </el-container>
    <div class="campus-enterprise-descriptions">
      <!-- 描述列表 -->
      <el-descriptions
        :title="titleVal"
        :column="2"
        class="campus-enterprise-description"
      >
        <template slot="extra">
          <el-button
            type="primary"
            size="small"
            @click="dialogFormVisible = true"
            >更新</el-button
          >
        </template>
        <el-descriptions-item label="我的头像">
          <el-upload
            class="avatar-uploader"
            :action="actionUrl"
            :show-file-list="false"
            :on-change="handleChange"
            multiple
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-descriptions-item>
        <el-descriptions-item label="我叫什么">{{
          enterpriseUserForm.hname
        }}</el-descriptions-item>
        <el-descriptions-item label="我的昵称">{{
          enterpriseUserForm.hnickname
        }}</el-descriptions-item>
        <el-descriptions-item label="我的年龄">{{
          enterpriseUserForm.hage
        }}</el-descriptions-item>
        <el-descriptions-item label="我的性别">{{
          enterpriseUserForm.hsex
        }}</el-descriptions-item>
        <el-descriptions-item label="我的号码">{{
          enterpriseUserForm.hphone
        }}</el-descriptions-item>
        <el-descriptions-item label="我的邮箱">{{
          enterpriseUserForm.email
        }}</el-descriptions-item>
        <el-descriptions-item label="我的公司">
          {{ enterpriseUserForm.company }}</el-descriptions-item
        >
      </el-descriptions>
      <!-- 按钮 -->
      <div class="campus-enterprise-edit">
        <el-button type="primary" plain>修改</el-button>
      </div>
    </div>
    <!-- 更新对话框 -->
    <el-dialog
      title="更新"
      :visible.sync="dialogFormVisible"
      modal
      :append-to-body="true"
    >
      <el-form :model="enterpriseUserForm" :inline="true">
        <el-form-item label="我的名称" :label-width="formLabelWidth">
          <el-input
            v-model="enterpriseUserForm.hname"
            autocomplete="off"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="我的昵称" :label-width="formLabelWidth">
          <el-input
            v-model="enterpriseUserForm.hnickname"
            autocomplete="off"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="我的年龄" :label-width="formLabelWidth">
          <el-input
            v-model="enterpriseUserForm.hage"
            autocomplete="off"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="我的性别" :label-width="formLabelWidth">
          <el-input
            v-model="enterpriseUserForm.hsex"
            autocomplete="off"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="我的号码" :label-width="formLabelWidth">
          <el-input
            v-model="enterpriseUserForm.hphone"
            autocomplete="off"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="我的邮箱" :label-width="formLabelWidth">
          <el-input
            v-model="enterpriseUserForm.email"
            autocomplete="off"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="我的公司" :label-width="formLabelWidth">
          <el-tooltip
            class="item"
            effect="dark"
            content="不可修改"
            placement="top-start"
          >
            <el-input
              :readonly="true"
              v-model="enterpriseUserForm.company"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateEnterpriseInfo"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 底部 -->
    <el-container>
      <el-footer>
        <div>
          <span>专注于互联网校招</span>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "RecommendEnterpriseuser",

  data() {
    return {
      // 个人信息展示表单
      enterpriseUserForm: {
        hid: 0,
        hname: "",
        hnickname: "",
        hpassword: "",
        hage: "",
        hsex: "",
        hphone: "",
        email: "",
        company: "",
      },
      titleVal: "",
      dialogFormVisible: false,
      formLabelWidth: "120px",
      actionUrl: "http://127.0.0.1:8088/enterprise/upload",
      imageUrl: "",
      saveImageUrl: {
        imgUrl: "",
      },
    };
  },

  mounted() {
    this.queryEnterpriseUserInfo();
    this.loadHeadImg();
  },
  methods: {
    // 加载个人数据
    queryEnterpriseUserInfo() {
      this.$axios
        .post("/enterprise/queryEnterpriseUserinfo")
        .then((res) => {
          if (res.data.result != null) {
            this.enterpriseUserForm = res.data.result;
            this.titleVal = res.data.result.hnickname;
          } else {
            return;
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
    // 更新个人信息方法
    updateEnterpriseInfo() {
      this.$axios
        .post("/enterprise/updateEnterpriseInfo", this.enterpriseUserForm)
        .then((res) => {
          var response = res.data.desc;
          if (response === "修改失败") {
            this.$message({
              message: "亲,更新失败请再试一下呢",
              type: "error",
            });
            return;
          } else if (response === "修改成功") {
            this.$message({
              message: "更新成功",
              type: "success",
            });
            // 关闭对话框
            this.dialogFormVisible = false;
            // 刷新数据
            this.queryEnterpriseUserInfo();
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
    // 加载头像
    loadHeadImg() {
      this.$axios
        .post("/enterprise/loadHeadImage")
        .then((res) => {
          if (res.data.desc != "") {
            this.imageUrl = res.data.desc;
          } else {
            return;
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
    handleChange(file) {
      if (file.response === "") {
        return;
      }
      this.imageUrl = file.response;
      this.saveImageUrl.imgUrl = this.imageUrl;
      this.$axios
        .post("/enterprise/save", this.saveImageUrl)
        .then((res) => {
          var response = res.data.desc;
          if (response === "保存失败") {
            return;
          } else if (response === "保存成功") {
            this.$message({
              message: "更新头像成功",
              type: "success",
            });
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: #409eff;
  color: white;
  line-height: 60px;
}

.el-footer {
  background-color: #409eff;
  color: white;
  line-height: 60px;
}

.campus-enterprise-descriptions {
  position: relative;
  margin-top: -10px;
  margin-bottom: 10px;
  height: 480px;
  background: whitesmoke;
  /* background-image: url(https://img1.baidu.com/it/u=4139099310,2309854459&fm=253&fmt=auto&app=138&f=JPEG?w=1000&h=500);
    background-size: 1355px 480px; */
}

.campus-enterprise-description {
  margin-left: 20px;
  padding-top: 20px;
  margin-top: 20px;
  margin-right: 20px;
}

.el-footer div {
  margin-left: 43%;
}

.campus-enterprise-edit {
  position: absolute;
  left: 1260px;
  top: 435px;
}

.item {
  margin: 4px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
